export type CounterState = {
    count: number
}

export const ActionTypes = {
    INCREMENT: 'INCREMENT',
    DECREMENT: 'DECREMENT',
    RESET: 'RESET'
} as const;

export type Action =
    | { type: typeof ActionTypes.INCREMENT }
    | { type: typeof ActionTypes.DECREMENT }
    | { type: typeof ActionTypes.RESET };

export const KEY = "key.count"

export const InitialState = (): CounterState => {
    try {
        const storage = localStorage.getItem(KEY);
        if (storage) {
            return JSON.parse(storage) as CounterState;
        }
    } catch {
        console.warn("Error reading local storage");
    }
    return { count: 0 };
}